/**
 * @program: LeetCode
 * @description: LeetCode : 剑指 Offer II 002. 二进制加法
 * @author: WXY
 * @create: 2022-12-18 20:29
 * @Version 1.0
 **/

public class offer_one2_addBinary {
    public static String addBinary(String a, String b) {
        if (a == null && b != null) {
            return b;
        }
        if (b == null && a != null) {
            return a;
        }
        int c = 0;
        StringBuilder sb = new StringBuilder();
        int i = a.length() - 1;
        int j = b.length() - 1;
        while (i >= 0 || j >= 0) {
            if (i >= 0) {
                c += a.charAt(i--) - '0';
            }
            if (j >= 0) {
                c += b.charAt(j--) - '0';
            }
            sb.append(c % 2);
            c >>= 1;
        }
        String s = sb.reverse().toString();
        return c > 0 ? "1" + s : s;
    }

}
